#include<stdio.h>
#include<unistd.h>
#include<stdarg.h>
#include"logger.h"

static FILE* log_file = NULL;

FILE* logger_init() {
 	log_file = fopen(LOGDIR "/gcpd.log", "a");
	setbuf(log_file, NULL);
	return log_file;
}

int logger_close() {
  if(log_file)
					fclose(log_file);
	log_file = NULL;
}

int logger_log(char *fmt, ...) {
	if(!log_file) logger_init();
	int result;
	va_list args;
	va_start(args, fmt);
	result = vfprintf(log_file, fmt, args);
	va_end(args);
	return result;
}

int clupsdLogMessage(int level, const char *fmt, ...) {
	if(!log_file) logger_init();
	int result;
	va_list args;
	va_start(args, fmt);
	fprintf(log_file, "%d: ", level);
	result = vfprintf(log_file, fmt, args);
	va_end(args);
	return result;
}
